package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsCategoryMapper;
import com.shop.pojo.DtsCategory;
import com.shop.service.DtsCategoryService;
import com.shop.vo.CatVo;
import com.shop.vo.CategoryAssist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName DtsCategoryServiceImpl
 * @Author xiao宋
 * @Date 20:52  2022/10/16
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsCategoryServiceImpl implements DtsCategoryService {
    @Autowired
    DtsCategoryMapper categoryMapper;

    @Override
    public List<CatVo> findCategoryList() {
        QueryWrapper<DtsCategory> w = new QueryWrapper<>();
        w.eq("deleted", 0);
//        找到所有类木
        List<DtsCategory> AllGoodsList = categoryMapper.selectList(w);

//          根据等级分类
//        循环等级  L1  的列表
        List<CatVo> catVoList = new ArrayList<>();
        for (DtsCategory c : AllGoodsList) {

//            根据 L1 的 ID 查到对应下面的 L2 类别
            List<DtsCategory> L2list = categoryMapper.selectList(new QueryWrapper<DtsCategory>().eq("pid", c.getId()));

            List<CategoryAssist> l = new ArrayList<>();
            if ("L1".equalsIgnoreCase(c.getLevel())) {
                CatVo v = new CatVo();
                v.setValue(c.getId());
                v.setLabel(c.getName());

                for (DtsCategory l2dtsCategory : L2list) {
                    CategoryAssist ca = new CategoryAssist();
                    ca.setLabel(l2dtsCategory.getName());
                    ca.setValue(l2dtsCategory.getId());
                    l.add(ca);
                    v.setChildren(l);
                }
                catVoList.add(v);
            }
        }

        return catVoList;
    }

    @Override
    public List<CategoryAssist> findL1List() {
        QueryWrapper<DtsCategory> w = new QueryWrapper<>();
        List<CategoryAssist> data = new ArrayList<>();
        w.eq("level", "L1");
        List<DtsCategory> list = categoryMapper.selectList(w);
        for (DtsCategory c : list) {
            CategoryAssist ca = new CategoryAssist();
            ca.setLabel(c.getName());
            ca.setValue(c.getId());
            data.add(ca);
        }
        return data;
    }

    @Override
    public DtsCategory findById(Integer categoryId) {
        return categoryMapper.selectById(categoryId);
    }

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        QueryWrapper<DtsCategory> w = new QueryWrapper<>();
        IPage<DtsCategory> iPage = new Page<>(page, limit);
        if (id != null) {
            w.eq("id", id);
//            w.eq(DtsCategory::getId, Id);
        }
        if (!StringUtils.isEmpty(name)) {
            w.eq("name", name);
        }
//        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }
        w.eq("deleted",false);
        categoryMapper.selectPage(iPage, w);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }

    @Override
    public boolean creatCategory(DtsCategory category) {
        category.setDeleted(false);
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        int count = 0;

        if ("L1".equalsIgnoreCase(category.getLevel())) {
            category.setPid(0);
            count = categoryMapper.insert(category);
        } else {
            count = categoryMapper.insert(category);
        }


        return count > 0;
    }

    @Override
    public void delete(DtsCategory category) {
//        LambdaQueryWrapper<DtsCategory> w=new LambdaQueryWrapper<>();

//        w.eq(DtsCategory::getDeleted,false);
        category.setDeleted(true);
        category.setUpdateTime(new Date());
        categoryMapper.updateById(category);
    }
}
